package design.graduation.novelsystem.controller;

import design.graduation.novelsystem.model.domian.Admin;
import design.graduation.novelsystem.model.dto.ActiveAdmin;
import design.graduation.novelsystem.model.dto.DataGridView;
import design.graduation.novelsystem.model.dto.ResultObj;
import design.graduation.novelsystem.model.vo.AdminVo;
import design.graduation.novelsystem.service.AdminService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author: 洪少尉
 * @Date: 2020/4/15 11:23
 * @Description: 管理员管理
 */
@RestController
@RequestMapping("admin")
public class AdminController {
	@Autowired
	private AdminService adminService;
	/**
	 * 全查询+条件查询
	 */
	@GetMapping("loadAllAdmin")
	@RequiresPermissions("admin:show")
	public Object loadAllAdmin(AdminVo adminVo){
		return this.adminService.queryAllAdmin(adminVo);
	}
	/**
	 * 添加用户
	 */
	@PostMapping("addAdmin")
	@RequiresPermissions("admin:add")
	public ResultObj addUser(Admin admin){
		if (this.adminService.saveAdmin(admin)){
			return ResultObj.ADD_SUCCESS;
		}
		return ResultObj.ADD_ERROR;
	}
	/**
	 * 返回管理员当前排序码的最大值
	 * 反写表单
	 */
	@GetMapping("queryAdminMaxSortCode")
	public Object queryAdminMaxSortCode(){
		Integer maxValue=this.adminService.queryAdminMaxSortCode();
		return new DataGridView(maxValue+1);
	}

	/**
	 * 修改用户
	 */
	@PostMapping("updateAdmin")
	@RequiresPermissions("admin:update")
	public ResultObj updateUser(Admin admin){
		if (this.adminService.updateAdmin(admin)){
			return ResultObj.UPDATE_SUCCESS;
		}
		return ResultObj.UPDATE_ERROR;
	}

	/**
	 * 重置用户密码
	 */
	@PostMapping("resetAdminPassword")
	public ResultObj resetAdminPassword(Integer id){
		if (this.adminService.resetAdminPassword(id)){
			return ResultObj.RESET_SUCCESS;
		}
		return ResultObj.RESET_ERROR;
	}

	/**
	 * 删除
	 */
	@RequestMapping("deleteAdmin")
	public ResultObj deleteUser(Integer id){
		if(this.adminService.removeAdminById(id)){
			return ResultObj.DELETE_SUCCESS;
		}
		return ResultObj.DELETE_ERROR;
	}

	/**
	 * 保存用户和角色之间的关系
	 */
	@RequestMapping("saveAdminRole")
	public ResultObj saveAdminRole(Integer aid,Integer[] rids){
		if(this.adminService.saveAdminRole(aid,rids)){
			return ResultObj.DISPATCH_SUCCESS;
		}
		return ResultObj.DISPATCH_ERROR;
	}

	/**
	 * 查询当前登陆的用户
	 */
	@GetMapping("getCurrentAdmin")
	public Object getCurrentAdmin(){
		ActiveAdmin activeAdmin = (ActiveAdmin) SecurityUtils.getSubject().getPrincipal();
		return new DataGridView(this.adminService.getBaseMapper().selectById(activeAdmin.getAdmin().getId()));
	}
	/**
	 * 判断当前密码
	 */
	@PostMapping("checkPassword")
	public ResultObj checkPassword(String password){
		if(this.adminService.checkPassword(password)){
			return ResultObj.SUCCESS_PASSWORD;
		}
		return ResultObj.ERROR_PASSWORD;
	}
}
